﻿using AutoMapper;
using Jinjia.Approve.Common.Extensions;
using Jinjia.Approve.IRepository;
using Jinjia.Approve.IService;
using Jinjia.Approve.Model;
using Jinjia.Approve.Model.Input.JJFlow;
using Jinjia.Approve.Model.VM.JJFlow;
using Jinjia.Approve.Service.Base;
using Jinjia.Core.BaseInfrastructure.ApiResultCodeComponent;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace Jinjia.Approve.Service
{
    /// <summary>
    /// 审批流模板远程接口维护
    ///</summary>
    public class JJFlowTemplateRemoteInterfaceService : BaseService, IJJFlowTemplateRemoteInterfaceService
    {
        private readonly IJJFlowTemplateRemoteInterfaceRepository _repository;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IMapper _mapper;

        public JJFlowTemplateRemoteInterfaceService(IJJFlowTemplateRemoteInterfaceRepository repository,
            IHttpClientFactory httpClientFactory,
            IMapper mapper)
        {
            _repository = repository;
            _httpClientFactory = httpClientFactory;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取下拉列表
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<JJFlowTemplateRemoteInterfaceDropDownListResponse>> GetDropDownListAsync()
        {
            return _mapper.Map<IEnumerable<JJFlowTemplateRemoteInterfaceDropDownListResponse>>(await _repository.GetListAsync(a => a.deleted == 0));
        }

        /// <summary>
        /// 获取远程接口数据
        /// </summary>
        /// <returns></returns>
        public async Task<R> GetRemoteInterfaceAsync<T, R>(RemoteInterfaceInput<T> input) where T : class
        {
            var remoteInterface = await _repository.GetFullFirstAsync(a => a.InterfaceCode == input.FunctionCode);

            if (!remoteInterface.IncludeContentFlag)
                input.Content = null;
            (RemoteInterfaceVM<R> result, HttpStatusCode code) response;

            switch (remoteInterface.Method)
            {
                case nameof(HttpMethod.Get):
                    response = await _httpClientFactory.CreateClient().GetAsync<RemoteInterfaceVM<R>>(remoteInterface.FullAddress, input.ToDictionary<string, string>());
                    break;

                case nameof(HttpMethod.Post):
                    response = await _httpClientFactory.CreateClient().PostAsync<RemoteInterfaceInput<T>, RemoteInterfaceVM<R>>(input, remoteInterface.FullAddress);
                    break;

                default:
                    throw new Exception($"目前只支持[{nameof(HttpMethod.Get)}]、[{nameof(HttpMethod.Post)}]请求");
            }

            if (response.code == HttpStatusCode.OK && response.result.res_status_code == ApiResultCode.Ok.ObjToInt().ToString())
            {
                return response.result.res_content;
            }

            throw new Exception(@$"远程接口请求编码:[{remoteInterface.InterfaceCode}];请求结果:{response.ToJson()}");
        }
    }
}